<div id="pageContainer">
</div>
<div class="index_bottom">
	<ul class="ul_hen" id="tabs">
		<?php $tabs = $this->tabs;if ($tabs['home']) { ?>
		<a href="javascript:void(0);" onclick="javascript:loadHomePage();return false;"><li tab-name="home"><span id="homePage"><img src="/images/homeGrey.png"></span><br>首页</li></a>
		<?php }?>
		<?php $tabs = $this->tabs;if ($tabs['category']) { ?>
		<a href="javascript:void(0);" onclick="javascript:loadCategoryPage();return false;"><li tab-name="category"><span id="categoryPage"><img src="/images/categoryGrey.png"></span><br>商城</li></a>
		<?php }?>
		<?php $tabs = $this->tabs;if ($tabs['presale']) { ?>
			<a href="javascript:void(0);" onclick="javascript:loadPreSalePage();return false;"><li tab-name="presale"><span id="presalePage"><img src="/images/presaleGrey.png"></span><br>鲜好货</li></a>
		<?php }?>
		<a href="javascript:void(0);" onclick="javascript:loadPersonalPage();return false;"><li tab-name="personal"><span id="personalPage"><img src="/images/personalGrey.png"></span><br>我的</li></a>
		<li tab-name="cart" style="padding-top:0px;margin-top:5px;position:relative;bottom:20px;" onclick="javascript:loadCartPage();return false;">
			<span class="cart_bg"><span class="cart_num" id="cart_num">0</span></span>
		</li>
	</ul>
</div>

<div id="loadingAnimation" style="display:none;width:100%;text-align:center;padding-top:150px;">
<img style="width:50%;" src="/images/loading2.gif"/>
</div>

<?php 
	if(!empty($this->tab)) {
		echo '<script>setCookie("tab","'.$this->tab.'");</script>';
	}
?>
<script type="text/javascript">
	loading = false;
	tmp = '';

	availableTabs = [];

	window.onunload = onunload_handler();
    function onunload_handler(){
		var tab = getCookie('tab');
		if(isNull(tab)){
			tab = 'home';
		}
		setCookie('tab',tab);
    }
	$(document).ready(function() {
		var tabList = $('.ul_hen li');
		if (tabList.length > 0) {
			var width = parseInt(99 / tabList.length);
			tabList.each(function(index,el){
				$(el).css('width',width+'%');
				availableTabs.push($(el).attr('tab-name'));
			});
		}

		loadTabsColor();
		loadCookie(0);
		loadPageAccordingly();
	});

	function loadPageAccordingly() {
		var tab = getCookie('tab');
		var loadHome = <?php echo  $tabs['home'];?>;
		var loadCategory = <?php echo  $tabs['category'];?>;
		var loadPresale = <?php echo  $tabs['presale'];?>;
		if (availableTabs.indexOf(tab) < 0) {
			tab = availableTabs[0];
			setCookie('tab',tab);
		}
		if(tab=='home' && loadHome){
			loadHomePage();
		}else if (tab == 'category' && loadCategory) {
			loadCategoryPage();
		}else if (tab == 'presale' && loadPresale) {
			loadPreSalePage();
		} else if (tab == 'personal') {
			loadPersonalPage();
		} else if (tab == 'cart') {
			loadCartPage();
		} 
	}

	function showContent (txt) {
		window.setTimeout( function() {
			loading = false;
			$('#loadingAnimation').hide();
			$('#pageContainer').html(txt);
		}, 500);
	}

	function isLoading() {
		return loading ? true : false;
	}

	function startLoading () {
		loading = true;
		$('#homePage').html('<img src="/images/homeGrey.png">');
		$('#categoryPage').html('<img src="/images/categoryGrey.png">');
		$('#presalePage').html('<img src="/images/presaleGrey.png">');
		$('#personalPage').html('<img src="/images/personalGrey.png">');
		tmp = $('#pageContainer').html();
		$('#pageContainer').html('');
		$('#loadingAnimation').show();
	}

	function loadHomePage() {
		if (isLoading())
			return false;
		startLoading();
		$('#homePage').html('<img src="/images/homeGreen.png">');
		$.ajax({
		  type: "get",
		  url : "/index/home",
		  success: function(txt){
			setCookie('tab','home');
			if (txt != '') {
				showContent(txt);
			}
		  },
		  error: function(obj,err,exp){
			showContent(tmp);
		  }
		});
	}

	function loadCategoryPage(el) {
		if (isLoading())
			return false;
		startLoading();
		var hotid = '';
		if(!isNull($(el).attr('hotid'))){
			hotid = $(el).attr('hotid');
		}
		if(!isNull(getCookie('brid'))){
			hotid= parseInt(getCookie('brid'));
		}
		$('#categoryPage').html('<img src="/images/categoryGreen.png">');
		$.ajax({
		  type: "get",
		  url : "/index/categroy",
		  data :"bid="+hotid,
		  datatype:'text',
		  success: function(txt){
			if (txt != '' && txt !='shopidIsNull') {
				setCookie('tab','category');
				showContent(txt);
			} else {
				showContent(tmp);
				// loadHomePage();
			}
		  },
		  error: function(obj,err,exp){
			showContent(tmp);
		  }
		});
	}

	function loadPreSalePage() {
		if (isLoading())
			return false;
		startLoading();

		$('#presalePage').html('<img src="/images/presaleGreen.png">');
		$.ajax({
			type: "get",
			url : "/index/presale",
			dataType:'text',
			success: function(txt){
				if (txt != '' && txt !='shopidIsNull') {
					setCookie('tab','presale');
					showContent(txt);
				} else {
					showContent(tmp);
					// loadHomePage();
				}
			},
			error: function(obj,err,exp){
				showContent(tmp);
			}
		});

	}

	function loadPersonalPage(){
		if (isLoading())
			return false;
		startLoading();

		$('#personalPage').html('<img src="/images/personalGreen.png">');
		$.ajax({
		  type: "get",
		  url : "/index/personal",
		  success: function(txt){
			if (txt != '') {
				setCookie('tab','personal');
				showContent(txt);
			}
		  },
		  error: function(obj,err,exp){
			showContent(tmp);
		  }
		});
	}

	function loadCartPage(){
		if (isLoading())
			return false;
		clearCookie('brid');
		clearCookie('starty');
		clearCookie('starty_brid');
		var cook = getCookie('cartCountPrice');
		var cartCountPriceList = cookToArr(cook);
		if(isNull(cartCountPriceList)){
			alert('亲，您的购物车空空如也！');
			return false;
		}

		startLoading();
		var cartCountPriceStr = getCookie('cartCountPrice');
		$.ajax({
		  type: "get",
		  url : "/index/cart",
		  data:{'cart':cartCountPriceStr},
		  success: function(txt){
			if (txt != '' && txt !='shopidIsNull') {
				setCookie('tab','cart');
				showContent(txt);
			}else{
				showContent(tmp);
			}
		  },
		  error: function(obj,err,exp){
			showContent(tmp);
		  }
		});
	}
	function locateFailedHandler(err){
		switch(err.code){
		case 1:
			alert("位置服务被拒绝");
			break;
		case 2:
			alert("暂时获取不到位置信息");
			break;
		case 3:
			alert("获取信息超时");
			break;
		default:
			alert("定位失败，请手动输入地址");
			break;
		}
		location.href="/address";
	}

	function locateSuccessHandler(position){
		var latitude = position.coords.latitude;
		var longitude = position.coords.longitude;
		$.ajax({
			  type: "post",
			  url : "/index",
			  dataType:'json',
			  data:{'lat':latitude,'lon':longitude},
			  success: function(info){
				$('#loadingAnimation').hide();
				var res = info.msg;
				if(res=="noData"){
					location.href="/address";
				}else{
					$('#locatingImg').hide();
					$('.index_bottom').show();
					loadHomePage();
				}
			  },
			  error: function(obj,err,exp){
					location.href="/address";
			  }
			});
	}
	
</script>